<template>
	<view v-if="goods_info.name" style="padding-bottom: 50px;">
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true" style="height: 280px;">
			<swiper-item v-for="(item, i) in goods_info.pictures" :key="i" style="height: 280px;">
				<image :src="item" style="height: 280px; width: 100%;" @click="preview(i)"></image>
			</swiper-item>
		</swiper>
			
		<view style="padding: 10px; padding-right: 0; border-bottom: 2px solid #efefef;">
			<view style="display: flex; justify-content: space-between; align-items: center; margin: 0 20px;">
				<view style="color: red; font-size: 20px; font-weight: bold; margin-bottom: 5px;">￥{{goods_info.price | tofixed}}</view>
				
			</view>

			<view style="display: flex; align-items: center; justify-content: space-between; margin: 0 20px; ">
				<view style="font-weight: bold; font-size: 14px;">商品名称：{{goods_info.name}}</view>
				<view style="font-weight: bold; font-size: 14px;">卖家{{goods_info.contact_way_category}}: {{goods_info.contact_way}}</view>
			</view>

		</view>	
		
		<view style="margin-top: 10px; margin-bottom: 5px; display: flex; align-items: center;">
			<uni-icons type="shop-filled" size="18"></uni-icons>
			<text style="font-size: 13px; margin-left: 2px; font-weight: bold;">商品详情</text>
		</view>
		
		<view style="margin-left: 5px;">{{goods_info.description}}</view>
		
		<view style="position: fixed; bottom: 0; left: 0; width: 100%;">
			<uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
		</view>
			
	</view>
</template>

<script>
	
	import { mapState, mapMutations, mapGetters } from 'vuex'
	
	export default {
		computed: {
			...mapState('m_cart', ['cart']),
			...mapState('m_my', ['user']),
			...mapState('m_my', ['user_id']),
			...mapGetters('m_cart', ['total']),
			...mapGetters('m_cart', ['pay_total']),
		},
		data() {
			return {
				id: "",
				animation: {},
				goods_info: {},
				options: [{
							icon: 'chat',
							text: '聊天',
						}, {
							icon: 'cart',
							text: '购物车',
							info: 0
						}],
				buttonGroup: [{
				  text: '加入购物车',
				  backgroundColor: '#ff0000',
				  color: '#fff'
				},
				{
				  text: '立即购买',
				  backgroundColor: '#ffa200',
				  color: '#fff'
				}],
			};
		},
		onLoad(e) {
			this.id = e.goods_id
	
			this.cha()
			//this.getgoodsinfo(goods_id)
			this.options[1].info = this.pay_total
		},
		onPullDownRefresh() {
			uni.stopPullDownRefresh()
		},
		methods: {
			...mapMutations('m_cart', ['addtoCart']),  //把store中的m_cart映射过来
			async cha(){
				const db = uniCloud.database();
				//用await保证函数彻底执行完，数据返回完毕后再执行下面的代码,(1),取第一条数据，get(),执行查询操作
				this.res = await db.collection("category").get();
			
				//获得的数据藏在res.result.data里，取值就行了
				this.goods_info = this.res.result.data.find(x => x._id === this.id) 
				
				console.log(this.goods_info)
			},
			
		/*	async getgoodsinfo(goods_id) {
				const {data: res} = await uni.$http.get('/api/public/v1/goods/detail', {goods_id})
				if(res.meta.status == 200){
					res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display: block;" ').replace(/webp/g, 'jpg')
					this.goods_info = res.message 
				}else{
					return uni.$failmsg()
				}
			},*/
			preview(i) {
				uni.previewImage({
					current: i,
					urls: this.goods_info.pictures
				})
			},
			
		    onClick (e) {
			  if(e.content.text == '购物车'){
				  uni.switchTab({
				  	url: '/pages/cart/cart'
				  })
			  }else if(e.content.text == '聊天'){
				if(JSON.stringify(this.user) == '{}') return uni.showToast({
				  	title: '请先登录!',
				  	duration: 1500,
				  	icon:'none'
				})
				if(this.user_id == this.goods_info.openid) return uni.showToast({
				  	title: '你不能和自己聊天!',
				  	duration: 1500,
				  	icon:'error'
				})
				uni.navigateTo({
				  	url: '/subpkg/chat/chat?recv_id=' + this.goods_info.openid
				})
			  }
		    },
		    buttonClick (e) {
				if(e.content.text == '加入购物车'){
					const goods = {
						goods_id: this.goods_info._id,
						goods_name: this.goods_info.name,
						goods_price: this.goods_info.price,
						goods_count: 1,
						goods_img: this.goods_info.pictures[0],
						goods_state: true
					}
					console.log(goods)
					if(JSON.stringify(this.user) == '{}') return uni.showToast({
						title: '请先登录!',
						duration: 1500,
						icon:'none'
					})
					this.addtoCart(goods)
					this.options[1].info = this.pay_total
				}
		    }
		},
		filters: {
			tofixed(num) {
				return Number(num).toFixed(2)
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: white;
	}
	.right {
		width: 120px;
		display: flex;
		flex-direction: column;
		align-items: center;
		font-size: 12px;
		justify-content: center;
		border-left: 2px solid #efefef;
	}
	
	.content {
		animation: zy 2.5s .15s linear infinite;
		animation: zy 2.5s .15s linear infinite;
		animation: zy 2.5s .15s linear infinite; 
		animation: zy 2.5s .15s linear infinite;
		width: 50px;
		height: 50px;
	}

	
	@-webkit-keyframes zy{ 
		10% { 
		  transform: rotate(15deg); 
		} 
		20% { 
		  transform: rotate(-10deg); 
		} 
		30% { 
		  transform: rotate(5deg); 
		} 
		40% { 
		  transform: rotate(-5deg); 
		} 
		50%,100% { 
		  transform: rotate(0deg); 
		} 
	}


</style>
